﻿using System;
using Eisk.Helpers;
namespace Eisk.BusinessEntities
{
    partial class Employee
    {
        const string nullValueErrorMessage = "Data field is not allowed to be null";
        const string emptyValueErrorMessage = "Data field is not allowed to be empty";

        //primary key, required field
        partial void OnEmployeeIdChanging(global::System.Int32 value)
        {
            if (value == EmployeeIdMinValue) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //required field
        partial void OnLastNameChanging(global::System.String value)
        {
            if (value.IsNull()) throw new InvalidOperationException(nullValueErrorMessage);
            else if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //required field
        partial void OnFirstNameChanging(global::System.String value)
        {
            if (value.IsNull()) throw new InvalidOperationException(nullValueErrorMessage);
            else if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        /// <summary>
        /// Validation for optional field
        /// For Optional data fields, when no data is provied, those are stored in database as NULL.
        /// Thus no empty values are allowed for optional data fields.
        /// </summary>
        /// <param name="value"></param>
        partial void OnTitleChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnTitleOfCourtesyChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnBirthDateChanging(global::System.DateTime? value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //required field
        partial void OnHireDateChanging(global::System.DateTime value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //required field
        partial void OnAddressChanging(global::System.String value)
        {
            if (value.IsNull()) throw new InvalidOperationException(nullValueErrorMessage);
            else if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnCityChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnRegionChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnPostalCodeChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //required field
        partial void OnCountryChanging(global::System.String value)
        {
            if (value.IsNull()) throw new InvalidOperationException(nullValueErrorMessage);
            else if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //required field
        partial void OnHomePhoneChanging(global::System.String value)
        {
            if (value.IsNull()) throw new InvalidOperationException(nullValueErrorMessage);
            else if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //[Required(ErrorMessage = "Home Phone is requiered")]
        //public partial void global::System.String HomePhone { }

        //optional field
        partial void OnExtensionChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnPhotoChanging(global::System.Byte[] value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnNotesChanging(global::System.String value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }

        //optional field
        partial void OnReportsToChanging(global::System.Int32? value)
        {
            if (value.IsEmpty()) throw new InvalidOperationException(emptyValueErrorMessage);
        }
        
        /// <summary>
        /// optional field, with default value
        /// </summary>
        partial void OnPhotoPathChanged()
        {
            if (PhotoPath.IsEmpty() || PhotoPath.IsNull())
            {
                PhotoPath = "c:\\photos\\";
            }
        }

        //optional field
        partial void OnBirthDateChanged()
        {
            ValidateBirthDateAndHireDate();
        }

        //required field
        partial void OnHireDateChanged()
        {
            ValidateBirthDateAndHireDate();
        }
        
        /// <summary>
        /// validation method
        /// RULE: BirthDate should not be later than the HireDate
        /// </summary>
        void ValidateBirthDateAndHireDate()
        {
            if ((BirthDate != null) && (!HireDate.IsEmpty()) && (DateTime.Compare((DateTime)BirthDate, HireDate) >= 0))
                throw new InvalidOperationException("Exception!!! BirthDate should be earlier than HireDate!");
        }

        //#region Helper Property

        ///// <summary>
        ///// This property is being used by the crud methods, to validate EmployeeId parameter 
        ///// </summary>
        //public static int EmployeeIdMinValue
        //{
        //    get { return 0; }
        //}

        //#endregion

    }
}
